<?php
/**
 * @file individualDonor.php
 * Creates UI for managing individual donors. Allows user to view all
 * individuals in the database, or to view by the first letter of a donor's
 * last name.
 * @author Matt Vartanian
 * @version 10/2/2011
 */
include ('newHeader.php');
include ('../db-control/dbIndividual.php');
include ('../db-control/dbDonor.php');

individualDonor( );

/**
 * Performs function calls to display header and create list of letters for
 * donor last name viewing.
 */
function individualDonor( )
{
	if(isset($_GET['search']) && $_GET['search'] != "all"){
		displayIndividualsByLetter( );
	}
	if(isset($_POST['deleteDonor']) && isset($_POST['donorID'])) {
		deleteIndividual( );
	}
	displaySearchLetters( );
	displayForm( );
	if(isset($_GET['search']) && $_GET['search'] == "all" || !isset($_GET['search'])){
		displayAllIndividuals( );
	}
	displayUpdate( );
}

/**
 * Retrieves all users from database and creates array of objects corresponding
 * to each row in the table
 */
function displayIndividualsByLetter( )
{
	$letterChosen = $_GET['search'];
	$lastNamesArray = dbIndividual::getIndividualsByLastName($letterChosen);
	$objectArray = array();
	$objectArray = convertToIndObjArray($lastNamesArray);
	displayExisting($objectArray);
}

/**
 * Displays all individuals
 * @param $objectArray array of all individuals in database
 */
function displayAllIndividuals( )
{
	$individualObjectArray = array ( );
	$allIndArray = array( );
	dbIndividual::getAllIndividuals($allIndArray);
	if($allIndArray == "") {
		echo "No individual donors were found.";
	} else {
		$individualObjectArray = convertToIndObjArray($allIndArray);
	}
	
	$donorObjectArray= array( );
	$idArray = array( );
	foreach($individualObjectArray as $object){
		$idArray[] = $object->getDonorID( );
	}
	foreach($idArray as $id){
		$donorObjectArray[]=dbDonor::getDonorById($id);
	}
	$donorObjectArray = convertToDonorObjArray($donorObjectArray);
	$numOfIndividuals = count($individualObjectArray);
	displayExisting($individualObjectArray, $donorObjectArray, $numOfIndividuals);
}

/**
 * Deletes individuals that are selected
 */
function deleteIndividual( )
{
	$deleteConfirmationString = "";
	$toBeDeleted = $_POST['donorID'];
	dbDonor::getDonorById($toBeDeleted);
	dbDonor::deleteDonor($item);

				
				$deleteConfirmationString .= $object->getLastName() .", ".
				$object->getFirstName(). "<br />";
	
	echo "The following individuals have been removed from the database: <br/>
	$deleteConfirmationString";

}

/**
 * Creates array of objects with firstName, lastName, and id
 * @param $individualArray array resource returned by sql query
 */
function convertToIndObjArray($individualArray)
{
	$objectArray = array( );
	foreach($individualArray as $item) { // creates array of objects
		$objectArray[ ] = new Individual($item['donorID'],
		$item['firstName'],
		$item['lastName'] );
	}
	return $objectArray;
}

function convertToDonorObjArray($donorObjectArray)
{
	$objectArray = array( );
	foreach($donorObjectArray as $item) { // creates array of objects
		$objectArray[ ] = new Donor($item);
	}
	return $objectArray;
}
/**
 * Creates selection boxes next to names
 */
function selectionBox($objectArray)
{
	foreach($objectArray as $object){
		
		echo <<<_END
	 <input type="checkbox" name="individualId[]" value="{$object->getDonorID( )}" />
	 <a href = "addIndDonor.php?id={$object->getDonorID()}">
	 {$object->getLastName( )},  {$object->getFirstName( )}</a><br />
_END;
	}
	echo "</form>";
}


/**
 * Creates alphabetic links. If a search letter has been chosen, it's not
 * displayed as a link.
 */
function displaySearchLetters( )
{
	echo "To search by last name:  ";
	if(isset($_GET['search'])){
		if($_GET['search'] == "all"){
			echo "All- ";
		} else {
			echo "<a href=\"http://localhost/donate/view-control/individualDonor.php?search=all\">All</a>"."- ";
		}
	} else {
		echo "<a href=\"http://localhost/donate/view-control/individualDonor.php?search=all\">All</a>"."- ";
	}

	if(isset($_GET['search'])){
		for($i=65; $i<91; $i++){
			if($_GET['search'] == chr($i)){
				echo chr($i). " ";
			} else {
				echo "<a href=\"http://localhost/donate/view-control/individualDonor.php?search=".chr($i)."\">".chr($i)."</a>"."   ";
			}
		}
	} else {
		for($i=65; $i<91; $i++){
			echo "<a href=\"http://localhost/donate/view-control/individualDonor.php?search=".chr($i)."\">".chr($i)."</a>"."   ";
		}
	}
	echo "<hr/>";
}



function displayForm( )
{

echo <<<_END
<br /><br />
<table align="center">
	<tr>
		<td valign="top" height="20">
			<fieldset>
				<legend>Add New Donor</legend>
					<form action="individualDonor.php" method="post">
						<p><label for="firstName">First Name</label>      <input type="text" name="firstName" value=""/></p>
						<p><label for="lastName">Last Name</label>        <input type="text" name="lastName" value=""/></p>
						<p><label for="street">Street</label>    <input type="text" name="street" value=""/></p>
						<p><label for="city">City</label>         <input type="text" name="city" value=""/></p>
						<p><label for="zip">Zip Code</label>         <input type="text" name="zip" value=""/></p>
						<p><label for="phone">Phone Number</label>         <input type="text" name="phone" value=""/></p>
						<p><label for="fax">Fax Number</label>         <input type="text" name="fax" value=""/></p>
						<p><label for="email">Email Address</label>         <input type="text" name="email" value=""/></p>
						<p class="submit"><input type="submit" value="Save" name="submit" /></p>
					</form>
			</fieldset>
		</td>
	<td rowspan = "3" valign="top">
_END;
}


function displayExisting($individualObjectArray, $donorObjectArray, $numOfIndividuals)
{
	echo <<<_END
	<fieldset id="fieldset">
		<legend>All Donors In System:</legend>
_END;
			for($i=0; $i<$numOfIndividuals; $i++){
	echo <<<_END
	<pre>
	<strong>First Name </strong>     {$individualObjectArray[$i]->getFirstName()}
	<strong>Last Name </strong>      {$individualObjectArray[$i]->getLastName()}
	<strong>Street </strong>         {$donorObjectArray[$i]->getStreet()}
	<strong>City </strong>           {$donorObjectArray[$i]->getCity()}
	<strong>Zip Code </strong>       {$donorObjectArray[$i]->getZip()}
	<strong>Phone Number </strong>   {$donorObjectArray[$i]->getPhone()}
	<strong>Fax Number </strong>     {$donorObjectArray[$i]->getFax()}
	<strong>Email Address </strong>  {$donorObjectArray[$i]->getEmail()}
	</pre>
	<form action="individualDonor.php" method="post">
	<input type="hidden" name="donorID" value="{individualObjectArray[$i]->getDonorID()}" />
	<center><br /><input type="submit" value="Delete Donor" name="deleteDonor" /><input type="submit" value="Edit Donor" name="editDonor" /></center>
	</form>
	<hr width=400px align="center" /><br />
_END;
	}
}


function displayUpdate( )
{
	displayEdit( );
	echo <<<_END
	</fieldset>
	</td>
	</tr>
	</table>
_END;
}

function displayEdit()
{
		echo <<<_END
			<tr>
	<td valign="top"><br />
	<fieldset>
	<legend>Edit Donor Information</legend>
		<form action="addStore.php" method="post">
		<p><label for="firstNameEdit">First Name</label> <input type="text" name="City2" value="" /></p>
		<p><label for="lastNameEdit">Last Name</label>   <input type="text" name="Address2" value="" /></p>
		<p><label for="streetEdit">Street</label>        <input type="text" name="Phone2" value="" /></p>
		<p><label for="cityEdit">City</label>            <input type="text" name="Manager2" value="" /></p>
		<p><label for="zipEdit">Zip Code</label>         <input type="text" name="City2" value="" /></p>
		<p><label for="phoneEdit">Phone Number</label>   <input type="text" name="City2" value="" /></p>
		<p><label for="faxEdit">Fax Number</label>       <input type="text" name="City2" value="" /></p>
		<p><label for="emailEdit">Email Address</label>  <input type="text" name="City2" value="" /></p>								
		<input type="hidden" value="" name="StoreID" />
		<p class="submit"><input type="submit" value="Save" name="send" /><input type="submit" value="Cancel" name="resetEdit" /></p>
		</form>
_END;
}